package com.gec.anan.customer.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.anan.customer.mapper.CustomerInfoMapper;
import com.gec.anan.customer.mapper.CustomerLoginLogMapper;
import com.gec.anan.customer.service.CustomerInfoService;
import com.gec.anan.model.entity.customer.CustomerInfo;
import com.gec.anan.model.entity.customer.CustomerLoginLog;
import com.gec.anan.model.form.customer.UpdateWxPhoneForm;
import com.gec.anan.model.vo.customer.CustomerLoginVo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {


    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private CustomerLoginLogMapper customerLoginLogMapper;

    /**
     * 获取用户在系统的id
     *
     * @param code
     * @return
     */
    @Override
    public Long login(String code) {
        String openid = null;
        //获取weixin的openid
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
            openid = sessionInfo.getOpenid();
            log.info("用户的openid{}", openid);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new RuntimeException("获取用户openid失败");
        }
        //查看是否是第一次登陆
        CustomerInfo customerInfo = getOne(new LambdaQueryWrapper<CustomerInfo>().eq(CustomerInfo::getWxOpenId, openid));
        if (customerInfo == null) {
            //添加用户信息
            customerInfo = new CustomerInfo();
            customerInfo.setNickname("新用户");
            customerInfo.setWxOpenId(openid);
            customerInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
            save(customerInfo);
        }
        //添加日志
        CustomerLoginLog customerLoginLog = new CustomerLoginLog();
        customerLoginLog.setCustomerId(customerInfo.getId());
        customerLoginLog.setMsg("小程序登录");
        customerLoginLogMapper.insert(customerLoginLog);

        return customerInfo.getId();
    }

    /**
     * 获取用户信息
     *
     * @param customerId
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {
        //调用方法
        CustomerInfo customerInfo = getById(customerId);
        //复制
        CustomerLoginVo customerLoginVo = new CustomerLoginVo();
        BeanUtils.copyProperties(customerInfo, customerLoginVo);
        //设置用户手机号是否已获取
        boolean isSuccess = StringUtils.hasText(customerInfo.getPhone());
        customerLoginVo.setIsBindPhone(isSuccess);
        return customerLoginVo;
    }

    /**
     * 更新手机号
     *
     * @param updateWxPhoneForm
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @SneakyThrows
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        //获取手机号结果
        //  WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(updateWxPhoneForm.getCode());
        //String phoneNumber = phoneNoInfo.getPhoneNumber();
        String phoneNumber = "123456789";
        log.info("手机号{}", phoneNumber);
        //更数据库信息
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setId(updateWxPhoneForm.getCustomerId());
        customerInfo.setPhone(phoneNumber);
        return updateById(customerInfo);
    }

    //获取乘客id
    @Override
    public String getCustomerOpenId(Long customerId) {
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerInfo::getId, customerId);
        CustomerInfo customerInfo = this.getOne(queryWrapper);
        return customerInfo.getWxOpenId();
    }
}
